package com.tms.control;

import com.tms.domain.Location;
import com.tms.domain.ProductLine;
import com.tms.domain.Tool;
import com.tms.service.LocationService;
import com.tms.service.ProductLineService;
import com.tms.service.ToolService;
import com.tms.service.UseRecordService;
import com.tms.utils.Data;
import com.tms.utils.JsonResult;
import com.tms.utils.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.tms.core.constants.ToolConstants.*;
import static com.tms.core.constants.ToolConstants.SUCCESS_INDOOR;

/**
 * 模具实体
 */
@RestController
@RequestMapping("/api/tools")
public class ToolController extends BaseController {

    @Autowired
    private ToolService toolService;

    @Autowired
    private UseRecordService useRecordService;

    @Autowired
    private LocationService locationService;

    @Autowired
    private ProductLineService productLineService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查看某个确定的tool
     *
     * @param toolId tool的id
     * @param request HttpServletRequest 获取token从而获取user
     * @return 成功返回 tool 信息，失败返回错误信息
     */
    @GetMapping
    public JsonResult viewTool(@RequestParam(value = "toolId") int toolId, HttpServletRequest request) {
        // 获取workcellId 用于判断是否是同一 workcell
        int workcellId = tokenService.getWorkcellId(request);
        if (workcellId == 0) {
            return new JsonResult(ERROR_TOKEN.getStatus(), ERROR_TOKEN.getDesc());
        }
        Tool tool = toolService.findToolById(toolId);
        // 判断是否取得了对应 id 的tool 或者 是否是同一 workcell
        if (tool == null) {
            return new JsonResult(ERROR_NULL_TOOL.getStatus(), ERROR_NULL_TOOL.getDesc());
        } else if (tool.getTDef().getWorkcell().getId() != workcellId) {
            return new JsonResult(ERROR_OTHER_WORKCELL.getStatus(), ERROR_OTHER_WORKCELL.getDesc());
        }
        return new JsonResult(SUCCESS.getStatus(), SUCCESS.getDesc(), new Data(tool));
    }

    /**
     * 获取 tool 的当前位置信息
     *
     * @param toolId  需要查询的 tool 的 id
     * @param request HttpServletRequest 获取token从而获取user
     * @return 成功返回所有的位置信息以及当前 tool的信息，失败返回错误信息
     */
    @GetMapping("/currentLocation/{id}")
    public JsonResult getCurrentLocation(@PathVariable(value = "id") int toolId, HttpServletRequest request) {
        // 用于判断是否是同一 workcell
        int workcellId = tokenService.getWorkcellId(request);
        if (workcellId == 0) {
            return new JsonResult(ERROR_TOKEN.getStatus(), ERROR_TOKEN.getDesc());
        }
        JsonResult jsonResult;
        Tool tool = toolService.findToolById(toolId);
        // 判断是否存在对应 id 的tool 或者 是否是同一 workcell
        if (tool == null) {
            jsonResult = new JsonResult(ERROR_NULL_TOOL.getStatus(), ERROR_NULL_TOOL.getDesc());
            return jsonResult;
        } else if (tool.getTDef().getWorkcell().getId() != workcellId) {
            return new JsonResult(ERROR_OTHER_WORKCELL.getStatus(), ERROR_OTHER_WORKCELL.getDesc());
        }
        List<ProductLine> productLines = productLineService.getAllProductLine();
        List<Location> locations = locationService.getAllLocation();
        switch (tool.getStatus().getId()) {
            case 2:
                jsonResult = new JsonResult(SUCCESS_OUTDOOR.getStatus(), SUCCESS_OUTDOOR.getDesc(), new Data("repairing", 0, locations, productLines));
                break;
            case 3:
                jsonResult = new JsonResult(SUCCESS_SCRAPED.getStatus(), SUCCESS_SCRAPED.getDesc(), new Data("scraped", 0, locations, productLines));
                break;
            case 4:
                jsonResult = new JsonResult(SUCCESS_REPAIR.getStatus(), SUCCESS_REPAIR.getDesc(), new Data("checking", 0, locations, productLines));
                break;
            case 6:
                jsonResult = new JsonResult(SUCCESS_INDOOR.getStatus(), SUCCESS_INDOOR.getDesc(), new Data("location", tool.getLocation().getId(), locations, productLines));
                break;
            case 1:
                ProductLine productLine = useRecordService.findLastRecordForToolId(toolId);
                jsonResult = new JsonResult(SUCCESS.getStatus(), SUCCESS.getDesc(), new Data("productLine", productLine.getNo(), locations, productLines));
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + tool.getStatus().getId());
        }
        return jsonResult;
    }
}